package com.crishne.anwo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crishne.anwo.constant.JsonStringConstant;
import com.crishne.anwo.model.Menu;
import com.crishne.anwo.model.User;
import com.crishne.anwo.service.MenuService;
import com.crishne.anwo.utill.CommonUtill;
import com.crishne.anwo.utill.PaginationUtil;

@Controller
@RequestMapping("sysmgr/menu/")
public class MenuController {
	
	@Autowired
	private MenuService menuService;
	
	private CommonUtill commonUtill = new CommonUtill();

	//返回jsp视图
	@RequestMapping(value = "menu_main")
	public String menu_main(){
		return "admin/sysmgr/menu_main";
	}
	
	@RequestMapping("getAllMenus")
	public void getUserMenuList(HttpServletResponse response,HttpServletRequest request)
	{
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(menuService.getAllMenus());
		commonUtill.OutputJson(jsonString,response,request);
	}
	
	@RequestMapping("getMenuPageList")
	public void getMenuPageList(HttpServletResponse response,HttpServletRequest request){
		//获取当前页 和 一页显示的数据数目
		String conCurrentPage = request.getParameter("currentpage");
		String stPageSize = request.getParameter("pageSize");
		String stMenuName = request.getParameter("menuname");
		Integer currentpage = 1;
		Integer pageSize = 10;
		if(conCurrentPage != null && !"".equals(conCurrentPage)){
			currentpage = Integer.valueOf(conCurrentPage);
		}
		
		if(stPageSize != null && !"".equals(stPageSize)){
			pageSize = Integer.valueOf(stPageSize);
		}
		
		
		@SuppressWarnings("rawtypes")
		HashMap searchCondition = new HashMap();
		searchCondition.put("condDefault","1=1");
		if(null !=stMenuName && !"".equals(stMenuName) ){
			searchCondition.put("condMenuName","st_menu_name like '%"+stMenuName+"%'");
		}
		//构造 PaginationUtil
		PaginationUtil<Menu> pageUtil = new PaginationUtil<Menu>();
		pageUtil.setCurrentPage(currentpage);
		pageUtil.setPageSize(pageSize);
		pageUtil.setStOrderBy(" nm_order asc");
		pageUtil.setSearchCondition(searchCondition);
		PaginationUtil<Menu> menuPageList = menuService.searchMenus(pageUtil);
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(menuPageList);

		commonUtill.OutputJson(jsonString, response, request);
    }
	
	@RequestMapping("getMenuEdit")
	public void getMenuEdit(HttpServletResponse response,HttpServletRequest request)
	{
		String menuId = request.getParameter("menuid");
		Menu tMenu = menuService.selectMenuById(menuId);
		
		HashMap retData = new HashMap();
		retData.put("menuinfo", tMenu);
		retData.put("allmenus", menuService.getAllMenus());
		JSONObject jsonString = JsonStringConstant.getSuccessMessage(retData);
		commonUtill.OutputJson(jsonString,response,request);
	}
    
    @RequestMapping("saveOrEditMenu")
	public void saveOrEditMenu(HttpServletResponse response,HttpServletRequest request){
		//获取参数
    	String menuId = request.getParameter("menuId");
		String menuName = request.getParameter("menuName");
		String menuUrl = request.getParameter("menuUrl");
		String stPid = request.getParameter("stPid");
		String menuOrder = request.getParameter("menuOrder");
		String stImage = request.getParameter("stImage");
		
		//不允许重名
		List<Menu> menuList = menuService.getMenusByName(menuName);
		if(null == menuId || "".equals(menuId)){
			if(null != menuList && menuList.size()>0){
				JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("该菜单名已经存在，保存失败！");
				commonUtill.OutputJson(jsonString, response, request);
				return;
			}
		}
		else{
			if(null != menuList && menuList.size()>0){
				for(Menu chekMenu :menuList){
					if(!chekMenu.getStMenuId().equals(menuId)){
						JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("该菜单名已经存在，保存失败！");
						commonUtill.OutputJson(jsonString, response, request);
						return;
					}
				}
			}
		}
		
		Menu tMenu = null;
		if(null == menuId || "".equals(menuId)){
			tMenu = new Menu();
		}
		else{
			tMenu = menuService.selectMenuById(menuId);
		}
		
		tMenu.setStMenuName(menuName);
		tMenu.setStMenuUrl(menuUrl);
		tMenu.setStPid(stPid);
		if(null != menuOrder && !"".equals(menuOrder)){
			tMenu.setNmOrder(Integer.valueOf(menuOrder));
		}
		else{
			tMenu.setNmOrder(0);
		}
		
		tMenu.setStImage(stImage);
		
		if(null == menuId || "".equals(menuId)){
			tMenu.setStMenuId(UUID.randomUUID().toString());
			tMenu.setStStatus("0");
			menuService.addMenu(tMenu);
		}
		else{
			menuService.updateMenu(tMenu);
		}
		
		JSONObject jsonString = JsonStringConstant.getNoDataSuccessMessage("保存成功！");

		commonUtill.OutputJson(jsonString, response, request);
    }
    
    @RequestMapping("deleteMenu")
    public void deleteMenu(HttpServletResponse response,HttpServletRequest request){
    	String stMenuIds = request.getParameter("menuId");
    	JSONObject jsonString = new JSONObject();
    	try{
    		menuService.deleteMenu(stMenuIds);
    		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除成功！");
    	}
    	catch(Exception ex){
    		jsonString = JsonStringConstant.getNoDataSuccessMessage("删除失败！");
    	}
    	
		commonUtill.OutputJson(jsonString, response, request);
    }

}